package com.c6906.vdam.dps.controller;

import com.c6906.vdam.dps.dto.ResultInfo;
import com.c6906.vdam.dps.dto.ResultUtil;
import com.c6906.vdam.dps.dto.TerminalDataListDTO;
import com.c6906.vdam.dps.entity.TerminalBusinessInfo;
import com.c6906.vdam.dps.entity.TerminalDMData;
import com.c6906.vdam.dps.entity.TerminalData;
import com.c6906.vdam.dps.entity.VehiclesLast;
import com.c6906.vdam.dps.service.TerminalBusinessInfoService;
import com.c6906.vdam.dps.service.TerminalDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Author : TAOHUIQI
 * @Date: 2024/6/15 20:49
 * @Description:
 */
@RestController
@RequestMapping({"/terminal"})
@Api(description = "设备处理Controller")
public class TerminalController {

    private static final Logger logger = LoggerFactory.getLogger(TerminalController.class);

    @Autowired
    TerminalBusinessInfoService terminalBusinessInfoService;

    @Autowired
    TerminalDataService terminalDataService;

    @GetMapping({"/initRedis"})
    @ApiOperation(value = "初始化达梦数据的设备信息到redis", notes = "初始化达梦数据的设备信息到redis")
    public ResultInfo<String> initRedis(HttpServletRequest request) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.initRedis());
    }

    @GetMapping({"/initIotDB"})
    @ApiOperation(value = "初始化达梦数据的设备信息到IotDB中创建Device", notes = "初始化达梦数据的设备信息到IotDB中创建Device")
    public ResultInfo<String> initIotDB(HttpServletRequest request) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.initIotDB());
    }

    @GetMapping({"/getTerminalBySNFromRedis"})
    @ApiOperation(value = "根据设备编号从redis获取单个设备基本信息", notes = "根据设备编号从redis获取单个设备基本信息")
    public ResultInfo<TerminalBusinessInfo> getTerminalBySNFromRedis(HttpServletRequest request, @Param("sn") String sn) throws Exception {
        TerminalBusinessInfo info = terminalBusinessInfoService.getTerminalBySNFromRedis(sn);
        return ResultUtil.getSuccessResult(info);
    }

    @GetMapping({"/getVehiclesLastMapInfoList"})
    @ApiOperation(value = "查询达梦数据库终端最新轨迹信息List", notes = "查询达梦数据库终端最新轨迹信息List")
    public ResultInfo<List<VehiclesLast>> getVehiclesLastMapInfoList(HttpServletRequest request, @Param("pageSize") int pageSize, @Param("pageNum") int pageNum) throws Exception {
        List<VehiclesLast> list = terminalBusinessInfoService.getVehiclesLastMapInfoList(pageSize, pageNum);
        return ResultUtil.getSuccessResult(list);
    }

    @GetMapping({"/queryTerminalBusinessBySNToRedis"})
    @ApiOperation(value = "根据设备编号从达梦获取单个设备信息并且缓存到redis", notes = "根据设备编号从达梦获取单个设备信息并且缓存到redis")
    public ResultInfo<List<TerminalBusinessInfo>> queryTerminalBusinessBySNToRedis(HttpServletRequest request, @Param("sn") String sn) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.queryTerminalBusinessBySNToRedis(sn));
    }

    @GetMapping({"/initRedisForList"})
    @ApiOperation(value = "初始化达梦数据的设备到Redis", notes = "初始化达梦数据的设备到Redis")
    public ResultInfo<List<TerminalBusinessInfo>> initRedisForList(HttpServletRequest request, @Param("snList") String snList) throws Exception {
        if (!StringUtils.isEmpty(snList)) {
            List<String> list = Arrays.asList(snList.split(","));
            return ResultUtil.getSuccessResult(terminalBusinessInfoService.initRedisForList(list));
        } else {
            return ResultUtil.getFailResult(null);
        }
    }

    @GetMapping({"/terminalIsExistFormRedis"})
    @ApiOperation(value = "根据设备编号判断redis中是否存在该设备信息", notes = "根据设备编号判断redis中是否存在该设备信息")
    public ResultInfo<String> terminalIsExistFormRedis(HttpServletRequest request, @Param("sn") String sn) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.terminalIsExistFormRedis(sn));
    }

    @PostMapping({"/updateBatchByTerminalSn"})
    @ApiOperation(value = "根据设备号批量更新达梦终端最新轨迹表", notes = "根据设备号批量更新达梦终端最新轨迹表")
    public ResultInfo<String> updateBatchByTerminalSn(HttpServletRequest request, @RequestBody TerminalDataListDTO dto) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.updateBatchByTerminalSn(dto.getTerminalDataList()));
    }

    @GetMapping({"/createDeviceTemplate"})
    @ApiOperation(value = "创建设备模板并且激活绑定数据库路径", notes = "创建设备模板并且激活绑定数据库路径")
    public ResultInfo<String> createDeviceTemplate(HttpServletRequest request) throws Exception {
        return ResultUtil.getSuccessResult(terminalBusinessInfoService.createDeviceTemplate());
    }

    @PostMapping({"/batchInsertDataToDM"})
    @ApiOperation(value = "批量插入数据到DM", notes = "批量插入数据到DM")
    public ResultInfo<String> batchInsertDataToDM(HttpServletRequest request, @RequestBody List<TerminalDMData> dataList) {
        return ResultUtil.getSuccessResult(terminalDataService.batchInsertDataToDM(dataList));
    }

    @PostMapping({"/batchInsertDataMQToDM"})
    @ApiOperation(value = "模拟MQ批量插入数据到DM", notes = "模拟MQ批量插入数据到DM")
    public ResultInfo<String> batchInsertDataMQToDM(HttpServletRequest request, @RequestBody List<TerminalData> dataList) {
        return ResultUtil.getSuccessResult(terminalDataService.batchInsertDataMQToDM(dataList));
    }
}
